'use strict';
const appConfig = {
  appid: 'wxf7abe114e4e3be3f',
  secret: '10688d3dd3c4f3414c9d95edc19369d0'
};

exports.main = async (event, context) => {
  const { code } = event;
  try {
    // 调用微信接口获取openid
    const result = await uniCloud.httpclient.request(
		`https://api.weixin.qq.com/sns/jscode2session?appid=${appConfig.appid}&secret=${appConfig.secret}&js_code=${code}&grant_type=authorization_code`,
		{
			dataType: 'json'
		}
    );
    console.log("获取openid信息:",result.data)
    if (result.data.openid) {
      return {
        code: 0,
        openid: result.data.openid
      };
    } else {
      throw new Error('获取openid失败');
    }
  } catch (e) {
    console.error('获取openid失败:', e);
    return {
      code: -1,
      msg: '获取openid失败'
    };
  }
}; 